<?php

class Clicktag extends CMS_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->config('banner/parametros');
        $this->load->language('banner/banner');
        $this->load->model('banner/banner_model');
    }
    
    public function index($id){
        $banner = $this->banner_model->getRandomBanner($id)->row();
        header("Content-type: text/javascript");
        $conteudo = "<a href='".base_url()."banner/clicktag/link/".$banner->id."'><img src='".  base_url() ."imagens/img/thumb/".$this->view($banner->id,false)."/80/728/90' alt='".$banner->titulo."' /></a>";
        
        
        ttp://motocerto.com.br/imagens/img/thumb/159/60/262.666666667/197
        echo 'document.write("'.$conteudo.'")';
    }

    /// Retornar a imagem se quiser que ao invés de flash retornar apenas imagem chame o metodo com flash=false
    function view($id, $flash=true) {

        if ($this->input->server('HTTP_REFERER')) {
            $url_last = $this->input->server('HTTP_REFERER');
        } else {
            $url_last = '';
        }

        $array = array(
            'banner_id' => $id,
            'tipo' => '0', // 1= clique, 0 = views
            'ip' => $this->input->ip_address(),
            'url_referencia' => $url_last
        );
        $banner = $this->banner_model->inserirStats($array);

        $banner = $this->banner_model->getBanner($id)->row();
        if ($banner->tipo == ".swf" && $flash) {
            return $banner->flash;
        } else {
            return $banner->imagem;
        }
    }
    
    public function link($id){
    if ($this->input->server('HTTP_REFERER')) {
            $url_last = $this->input->server('HTTP_REFERER');
        } else {
            $url_last = '';
        }

        $array = array(
            'banner_id' => $id,
            'tipo' => '1', // 1= clique, 0 = views
            'ip' => $this->input->ip_address(),
            'url_referencia' => $url_last
        );
        $banner = $this->banner_model->inserirStats($array);

        $banner = $this->banner_model->getBanner($id)->row();
        redirect($banner->link);
    }
}